package posts

import (
	"context"

	"mall/common/jwt"
	"mall/common/mqueuex"
	"mall/common/resultx"
	"mall/common/utils/pointy"
	"mall/service/forum/api/internal/svc"
	"mall/service/forum/api/internal/types"
	"mall/service/forum/rpc/forumclient"
	"mall/service/user/rpc/userclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type DeletePostsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 删除我的香评 ##25.03.31## 历史遗留文件，暂不处理
func NewDeletePostsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeletePostsLogic {
	return &DeletePostsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DeletePostsLogic) DeletePosts(req *types.IDsReq) (*types.BaseMsgResp, error) {
	if len(req.Ids) == 0 {
		return nil, resultx.NewErrCode(resultx.FORUM_POST_ID_REQUIRED)
	}

	userId := jwt.GetUserId(l.ctx)
	if userId == 0 {
		return nil, resultx.NewErrCode(resultx.TOKEN_EXPIRE_ERROR)
	}

	_, err := l.svcCtx.ForumRpc.DeletePosts(l.ctx, &forumclient.DeletePostReq{
		Ids:       req.Ids,
		UpdatedBy: pointy.GetPointer(userId),
	})
	if err != nil {
		return nil, err
	}

	var userMeta []*userclient.UserMetaInfo
	userMeta = append(userMeta, &userclient.UserMetaInfo{
		Key:   pointy.GetPointer(mqueuex.UserStatisticForumReviewNew), // "_review_new_status"
		Value: pointy.GetPointer("-1"),
	})
	l.svcCtx.UserRpc.SetUserMeta(l.ctx, &userclient.SetUserMetaReq{
		UserId:   pointy.GetPointer(userId),
		MetaKey:  pointy.GetPointer(mqueuex.UserStatisticForumReviewCount), // "_review_count"
		Incr:     pointy.GetPointer(int32(-1)),
		UserMeta: userMeta,
	})

	return &types.BaseMsgResp{
		Status:  "ok",
		Message: "删除成功",
	}, nil
}
